﻿using System;
using System.Collections.Generic;
using System.Text;

using com.vs.General;
using com.vs.DataAccess.DO;
using com.vs.Inventory.BL;
using com.vs.Task.BL;
using com.vs.General.BL;

namespace com.vs.Shipping.BL
{
    class WaveThread
    {
        Session sess = null;
        int iWaveNum = 0;
        WaveFlowHeaderDO wfhDO = null;
        List<WaveFlowDetailDO> ltFlowDetail;
        bool bAutoRelease = false;

        public WaveThread(Session sess, int iWaveNum, WaveFlowHeaderDO wfhDO, List<WaveFlowDetailDO> ltFlowDetail, bool bAutoRelease)
        {
            this.sess = sess;
            this.iWaveNum = iWaveNum;
            this.ltFlowDetail = ltFlowDetail;
            this.wfhDO = wfhDO;
            this.bAutoRelease = bAutoRelease;
        }

        public void Process()
        {
            string stError = null;
            bool bFatalError = false;
            WaveHelper waveHelper = null;

            try
            {
                waveHelper = new WaveHelper();

                foreach (WaveFlowDetailDO wfdDO in ltFlowDetail)
                {
                    switch (wfdDO.WAVE_STEP)
                    {
                        case "70": //开始波次
                            {
                                //WaveHelper helper = new WaveHelper();
                                stError = waveHelper.StartWave(sess, iWaveNum);
                                break;
                            }
                        case "80"://库存分配
                            {
                                AllocationHelper helper = new AllocationHelper();
                                helper.bCreateTask = (wfhDO.CREATE_TASK == "Y") ? true : false;
                                stError = helper.AllocateWave(sess, iWaveNum);
                                break;
                            }
                        case "50"://创建装载
                            {
                                ShippingLoadManager helper = new ShippingLoadManager();
                                stError = helper.CreateWaveLoad(sess, iWaveNum);
                                break;
                            }
                        case "30"://数据修改
                            {
                                break;
                            }
                        case "10"://补货
                            {
                                ReplenishmentHelper helper = new ReplenishmentHelper(sess);
                                helper.bCreateTask = (wfhDO.CREATE_TASK == "Y") ? true : false;
                                helper.ReplenishByWaveDemand(sess, iWaveNum);
                                break;
                            }
                        case "20"://创建补货任务
                            {
                                TaskGenerationHelper helper = new TaskGenerationHelper();
                                helper.CreateWaveTasksByMaster(sess, iWaveNum, TaskConstants.REPLENISH_TASK);
                                break;
                            }
                        case "40"://创建一般拣货任务
                            {
                                TaskGenerationHelper helper = new TaskGenerationHelper();
                                helper.CreateWaveTasksByMaster(sess, iWaveNum, TaskConstants.SHIP_ALLOC_TASK);
                                break;
                            }
                        case "60"://结束波次
                            {
                                //WaveHelper helper = new WaveHelper();
                                stError = waveHelper.CompleteWave(sess, iWaveNum);
                                break;
                            }
                        case "100"://指定分配规则
                            {
                                break;
                            }
                    }
                    //每阶段错误处理
                    if (!StringManager.IsEmpty(stError))
                    {
                        HistoryManager.SaveProcessHistory(sess,
                            HistoryManager.ProcessType.iWave,
                            HistoryManager.Condition.iFailed,
                            stError,
                            "" + iWaveNum, null, null, null);
                        bFatalError = true;
                        break;
                    }
                }

                if (!bFatalError && bAutoRelease)
                    stError = waveHelper.ReleaseWave(sess, iWaveNum);

                //如果波次出现异常，更新到波次的错误信息
                if (!StringManager.IsEmpty(stError))
                {
                }

                return;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
            }
            finally
            {
            }
        }
    }
}
